﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Prism.Ioc;
using Prism.Regions;
using YJTMedicalAideManage.Cores.Events;
using YJTMedicalAideManage.Cores.Globals;
using YJTMedicalAideManage.ViewModels;

namespace YJTMedicalAideManage.Views
{
    /// <summary>
    /// LoaginView.xaml 的交互逻辑
    /// </summary>
    public partial class LoginView
    {
        private readonly IContainerExtension _container;
        private readonly IRegionManager _regionManager;
        public LoginView(IContainerExtension container, IRegionManager regionManager)
        {
            InitializeComponent();
            LoginVisibilityEvent.Instance.Subscribe(LoginCommandMethod);
            _container = container;
            _regionManager = regionManager;
            PwdStringEvent.Instance.Subscribe(pwd);
            var viewmodel = new LoginViewModel();
            viewmodel.LoginSuccess += OnLoginSuccess;
            DataContext = viewmodel;
        }
        private void LoginCommandMethod(Visibility visibility)
        {
            if (visibility == Visibility.Collapsed)
            {
                GlobalLoadingMask.Visibility = Visibility.Collapsed;
            }
            else
            {
                GlobalLoadingMask.Visibility = Visibility.Visible;
            }
        }
        private void Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }
        private void OnLoginSuccess()
        {
            var mainWindow = _container.Resolve<MainWindow>();
            Close();
            mainWindow.Show();
        }
        private void textUser_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            txtUser.Focus();

        }
        private void txtUser_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUser.Text) && txtUser.Text.Length > 0)
            {
                textUser.Visibility = Visibility.Collapsed;
            }
            else
            {
                textUser.Visibility = Visibility.Visible;
            }
        }

        private void textPassword_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            txtPassword.Focus();
        }

        private void Border_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                //this.DragMove();
            }
        }

        private void txtPassword_TextChanged(object sender, RoutedEventArgs e)
        {
            PwdStringEvent.Instance.PushMessage(txtPassword.Password.ToString());
            
        }
        private void tttPassword_TextChanged(object sender, RoutedEventArgs e)
        {
            PwdStringEvent.Instance.PushMessage(tttPassword.Text.ToString());

        }

        private void Button_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {

            }
        }
        private void pwd(string str) 
        {
            if (str == "查看密码")
            {
                if (txtPassword.Visibility == Visibility.Visible)
                {

                    txtPassword.Password = GlobalApp.Instance.RemoveChineseCharacters(tttPassword.Text.ToString()); 
                }
                else
                {

                    tttPassword.Text = GlobalApp.Instance.RemoveChineseCharacters(txtPassword.Password.ToString());
                }
            }
            else if (str.Contains("设置密码"))
            {
                int index = str.IndexOf("设置密码") + 2;  // 找到 "密码" 的位置，加 2 是因为 "密码" 的长度是 2
                string password = str.Substring(index);
                txtPassword.Password = password;
                tttPassword.Text = password;
            }
           
        }
    }
}
